package com.david.controller.api;

import com.david.entity.Category;
import com.david.model.api.ArticleContent;
import com.david.model.api.Response;
import com.david.service.ArticleContentService;
import com.google.common.collect.ImmutableBiMap;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/articleContent")
public class ArticleContentController {
    @Autowired
    ArticleContentService articleContentService;


    @GetMapping("findAll")
    @ApiOperation(value = "获取全部", notes = "获取全部", tags = "articleContent")
    public Response<Object> findAll() {
        Response<Object> result = new Response<>();
        result.data = articleContentService.findAll();
        return result;
    }

    @GetMapping("findByparentIdType")
    @ApiOperation(value = "根据ParentId获取", notes = "根据ParentId获取", tags = "articleContent")
    public Response<Object> findByparentIdType(Integer parentId,String type) {
        Response<Object> result = new Response<>();
        result.data = articleContentService.findByparentIdType(parentId,type);
        return result;
    }

    @GetMapping("findById")
    @ApiOperation(value = "根据id获取", notes = "根据id获取", tags = "articleContent")
    public Response<Object> findById(Integer id) {
        Response<Object> result = new Response<>();
        result.data = articleContentService.findById(id);
        return result;
    }

    @GetMapping("delete")
    @ApiOperation(value = "删除", notes = "删除", tags = "articleContent")
    public Response<Map> delete(Integer id) {
        Response<Map> result = new Response<>();
        int data = articleContentService.delete(id);
        if (data > 0) {
            result.data = ImmutableBiMap.of("result", true);
        } else {
            result.data = ImmutableBiMap.of("result", false);
        }
        return result;
    }

    @PostMapping("update")
    @ApiOperation(value = "修改信息", notes = "修改信息", tags = "articleContent")
    public Response<Map> update(@RequestBody ArticleContent articleContent) {
        Response<Map> result = new Response<>();
        int data = articleContentService.update(articleContent);
        if (data > 0) {
            result.data = ImmutableBiMap.of("result", true);
        } else {
            result.data = ImmutableBiMap.of("result", false);
        }
        return result;
    }

    @PostMapping("insert")
    @ApiOperation(value = "添加信息", notes = "添加信息", tags = "articleContent")
    public Response<Map> insert(@RequestBody ArticleContent articleContent) {
        Response<Map> result = new Response<>();
        int insert = articleContentService.insert(articleContent);

        if (insert> 0) {
            result.data = ImmutableBiMap.of("result", true);
        } else {
            result.data = ImmutableBiMap.of("result", false);
        }

        return result;
    }

    @GetMapping("updateSort")
    public Response<Map> updateSort(Integer sort,Integer id){
        Response<Map> result = new Response<>();
        articleContentService.updateSort(sort,id);
        result.data = ImmutableBiMap.of("result", true);
        return result;
    }
}
